﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.Authentication.Library.Web.Framework;
using Resources;
using Hutech.Authentication.Library.Business.WebHelpers;
using log4net;
using Hutech.Authentication.Library.Business;
using System.Globalization;

namespace Hutech.CRM.Web.Admin
{
    public partial class RoleManager : hutechBasePage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(RoleManager));
        private static bool debugLog = log.IsDebugEnabled;

        protected string EditPropertiesImage = "~/Data/SiteImages/" + WebConfigSettings.EditPropertiesImage;
        protected string DeleteLinkImage = "~/Data/SiteImages/" + WebConfigSettings.DeleteLinkImage;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ImageButtonAddNew.Click += new ImageClickEventHandler(ImageButtonAddNew_Click);
            this.rolesList.ItemCommand += new DataListCommandEventHandler(RolesList_ItemCommand);
            this.rolesList.ItemDataBound += new DataListItemEventHandler(rolesList_ItemDataBound);
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!WebUser.IsAdminOrRoleAdmin)
            {
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }
            PopulateLabels();
            PopulateControls();
        }

        #region Load All data
        private void PopulateLabels()
        {
            Title = Resource.AdminMenuRoleAdminLink;
            spnTitle.InnerText = Resource.AdminMenuRoleAdminLink;

            lnkAdminMenu.Text = Resource.AdminMenuLink;
            lnkAdminMenu.ToolTip = Resource.AdminMenuLink;
            lnkAdminMenu.NavigateUrl = SiteRoot + "/Admin/AdminMenu.aspx";

            //lnkRoleAdmin.Text = Resource.AdminMenuRoleAdminLink;
            //lnkRoleAdmin.ToolTip = Resource.AdminMenuRoleAdminLink;
            //lnkRoleAdmin.NavigateUrl = SiteRoot + "/Admin/RoleManager.aspx";

            //SiteUtils.SetButtonAccessKey(btnAddRole, AccessKeys.RolesAddButtonAccessKey);
        }

        #endregion

        #region All event
        private void PopulateControls()
        {
            if (Page.IsPostBack)
                return;
            BindRoleList();
        }

        protected string FormatMemberLink(int memberCount)
        {
            return string.Format(Resource.RolesMemberLinkFormat, memberCount);
        }
        private void BindRoleList()
        {
            List<Role> siteRoles = Role.GetbySite();
            if (!WebUser.IsAdmin)
            {
                // must be only Role Admin
                // remove admins role and role admins role
                // from list. Role Admins can't edit or manage
                // membership in Admins role or Role Admins role
                foreach (Role r in siteRoles)
                {
                    if (r.Equals("Admins"))
                    {
                        siteRoles.Remove(r);
                        break;
                    }
                }

                foreach (Role r in siteRoles)
                {
                    if (r.Equals("Role Admins"))
                    {
                        siteRoles.Remove(r);
                        break;
                    }
                }
            }

            rolesList.DataSource = siteRoles;
            rolesList.DataBind();
        }

        void ImageButtonAddNew_Click(object sender, ImageClickEventArgs e)
        {
            if (this.txtNewRoleName.Text.Length > 0)
            {
                Role role = new Role();
                role.SiteID = Guid.Parse("cd348ff8-00c2-496b-8a65-5e78201f7780");
                role.RoleName = this.txtNewRoleName.Text;
                role.Save();

                //Clear data
                this.txtNewRoleName.Text = string.Empty;
                this.lblMessageError.Text = string.Empty;
                //Refresh data
                BindRoleList();
                this.pnlMain.Update();
                //WebUtils.SetupRedirect(this, Request.RawUrl);
            }
            else
                lblMessageError.Text = "Tên gọi lớn hơn 0 và nhỏ hơn 50 ký tự!";
            return;
        }
        protected void RolesList_ItemCommand(object sender, DataListCommandEventArgs e)
        {
            if (debugLog)
            {
                log.Debug("fired event RolesList_ItemCommand");
            }

            Guid roleID = new Guid(rolesList.DataKeys[e.Item.ItemIndex].ToString());
            Role role = new Role(roleID);
            switch (e.CommandName)
            {
                case "edit":
                    rolesList.EditItemIndex = e.Item.ItemIndex;
                    BindRoleList();
                    this.pnlMain.Update();
                    break;

                case "apply":
                    role.RoleName = ((TextBox)e.Item.FindControl("roleName")).Text;
                    role.Save();
                    rolesList.EditItemIndex = -1;
                    BindRoleList();
                    this.pnlMain.Update();
                    break;
                case "delete":
                    //Cho nay se lam sau... Cho nay phai kiem tra dieu kien role do co con cap quyen cho user nao hay khong
                    //Hoac kiem tra khong cho xoa cac quyen dac biet.  ................Giai phap?

                    Role.DeleteRole(roleID);
                    BindRoleList();
                    this.pnlMain.Update();
                    //WebUtils.SetupRedirect(this, Request.RawUrl);
                    break;
                case "cancel":
                    rolesList.EditItemIndex = -1;
                    BindRoleList();
                    this.pnlMain.Update();
                    //WebUtils.SetupRedirect(this, Request.RawUrl);
                    break;
            }
        }
        void rolesList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ImageButton ImageButtonDelete = e.Item.FindControl("ImageButtonDelete") as ImageButton;
            UIHelper.AddConfirmationDialog(ImageButtonDelete, Resource.RolesDeleteWarning);
        }
        #endregion
    }
}
